/*
 * @Author: 小豪
 * @Description:
 * @Date: 2024-04-29 10:53:54
 * @LastEditTime: 2024-04-29 10:57:27
 * @FilePath: \zkyjt\gohub\app\http\controllers\api\v1\auth\password_controller.go
 */
// Package auth 处理用户注册、登录、密码重置
package auth

import (
	v1 "gohub/app/http/controllers/api/v1"
	"gohub/app/models/user"
	"gohub/app/requests"
	"gohub/pkg/response"
	"time"

	"github.com/gin-gonic/gin"
)

// PasswordController 用户控制器
type PasswordController struct {
	v1.BaseAPIController
}

// ResetByPhone 使用手机和验证码重置密码
func (pc *PasswordController) ResetByPhone(c *gin.Context) {
	// 1. 验证表单
	request := requests.ResetByPhoneRequest{}
	errs := requests.Validate(c, &request, requests.ResetByPhone)
	if len(errs) > 0 {
		// 失败，显示错误提示
		response.JSON(c, gin.H{
			"success":    false,
			"total_page": "",
			"status":     "100",
			"msg":        errs,
			"data":       map[string]string{},
			"time":       time.Now().Unix(),
		})
		return
	}
	// 2. 更新密码
	userModel := user.GetByPhone(request.Phone)
	if userModel.ID == 0 {
		response.JSON(c, gin.H{
			"success":    false,
			"total_page": "",
			"status":     "100",
			"msg":        "该用户不存在！",
			"data":       map[string]string{},
			"time":       time.Now().Unix(),
		})
		return
	} else {
		userModel.Password = request.Password
		res := userModel.Save()
		if res == 0 {
			response.JSON(c, gin.H{
				"success":    false,
				"total_page": "",
				"status":     "100",
				"msg":        "修改失败！",
				"data":       map[string]string{},
				"time":       time.Now().Unix(),
			})
			return
		}
		response.JSON(c, gin.H{
			"success":    false,
			"total_page": "",
			"status":     "200",
			"msg":        "修改成功！",
			"data":       map[string]string{},
			"time":       time.Now().Unix(),
		})
		return
	}
}
